package com.future.mayi.fragment;

import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.future.baselib.entity.DefaultResponse;
import com.future.baselib.utils.ApiCst;
import com.future.baselib.utils.HttpRequest;
import com.future.mayi.R;
import com.future.mayi.activity.AppraisalOrderActivity;
import com.future.mayi.activity.BaseFragment;
import com.future.mayi.activity.CourierInfoActivity;
import com.future.mayi.activity.EdtExpressActivity;
import com.future.mayi.activity.EvaluationActivity;
import com.future.mayi.adapter.AppraisalAllOrderAdapter;
import com.future.mayi.app.App;
import com.future.mayi.dialog.ConfirmGoodsDialog;
import com.future.mayi.model.AppraisalOrderModel;
import com.future.mayi.model.interfaces.DialogEnterListener;
import com.future.mayi.model.interfaces.OnItemClickListener;
import com.future.mayi.response.AppraisalAllOrderResponse;
import com.future.mayi.utils.HCUtils;
import com.future.mayi.view.xrecyclerview.XRecyclerView;

import butterknife.BindView;

public class AppraisalItemOrderFragment extends BaseFragment implements XRecyclerView.LoadingListener, OnItemClickListener {

    private int style = 0;
    private int status = 0;

    private int page = 0;

    @BindView(R.id.recycler_view)
    XRecyclerView recyclerView;
    @BindView(R.id.empty_view)
    View emptyView;

    private AppraisalAllOrderAdapter adapter;

    @Override
    protected int getLayoutResource() {
        return R.layout.fragment_appraisal_item_order;
    }

    @Override
    protected void initView() {
        recyclerView.setHasFixedSize(true);
        final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        adapter = new AppraisalAllOrderAdapter(getActivity(), recyclerView, style);
        recyclerView.setAdapter(adapter);
        recyclerView.setEmptyView(emptyView);
    }

    @Override
    protected void initListener() {
        recyclerView.setLoadingListener(this);
        adapter.setOnItemClickListener(this);
    }

    @Override
    protected void initData() {
        recyclerView.refresh();
    }

    public void setStyle(int style, int itemStyle) {
        this.style = style;
//        {"全部", "待付款0", "待寄件10", "待鉴定11", "待收货20", "待评价30"};
        switch (itemStyle) {
            case 0:
                status = -1;
                break;
            case 1:
                status = 0;
                break;
            case 2:
                status = 10;
                break;
            case 3:
                status = 11;
                break;
            case 4:
                status = 20;
                break;
            case 5:
                status = 30;
                break;
        }
    }


    @Override
    public void OnItem(View view, int position) {
        AppraisalOrderModel model = adapter.getItem(position);
        if (model == null) return;
        if (view.getId() == R.id.tv_enter) {
            switch (model.getStatus()) {
                case 0:
                    break;
                case 10:
                    //填写寄件
                    EdtExpressActivity.startActivity(getActivity(), String.valueOf(model.getOrder_id()));
                    break;
                case 11:
                    //查物流
                    CourierInfoActivity.startActivity(getActivity(), String.valueOf(model.getOrder_id()), true, 0);
                    break;
                case 20:
                    //确认收货
                    confirm(model);
                    break;
                case 30:
                    //写评价
                    EvaluationActivity.startActivity(getActivity(), String.valueOf(model.getOrder_id()), true);
                    break;
                default:
                    break;
            }
            return;
        }
        AppraisalOrderActivity.startActivity(getActivity(), model, style);
    }


    @Override
    public void onRefresh() {
        page = 0;
        getData();
    }

    @Override
    public void onLoadMore() {
        page++;
        getData();
    }

    private void getData() {
        new HttpRequest<AppraisalAllOrderResponse>()
                .with(getActivity())
                .setApiCode(ApiCst.getListOtherorder)
                .addParam("p", page)
                .addParam("order_type", style)
                .addParam("status", status == -1 ? null : status)
                .addParam("psize", 10)
                .addParam("token", App.getToken(mContext))
                .setListener(new HttpRequest.OnNetworkListener<AppraisalAllOrderResponse>() {
                    @Override
                    public void onSuccess(AppraisalAllOrderResponse response) {
                        HCUtils.refreshListForPage(recyclerView, adapter, response.getList(), page, 10);
                    }

                    @Override
                    public void onFail(String message, AppraisalAllOrderResponse response) {
                        page = HCUtils.refreshFail(recyclerView, page, getContext(), message);
                    }
                }).start(new AppraisalAllOrderResponse());
    }


    @Override
    public void OnLongItem(View view, int position) {

    }


    /**
     * 确认收货
     */
    private void confirm(final AppraisalOrderModel model) {
        ConfirmGoodsDialog confirmGoodsDialog = new ConfirmGoodsDialog(getActivity());
        confirmGoodsDialog.setListener(new DialogEnterListener() {
            @Override
            public void cancel(Object tag) {

            }

            @Override
            public void enter(Object tag) {
                confirmOrCancel(40, String.valueOf(model.getOrder_id()));
            }
        });
        confirmGoodsDialog.show();
    }

    /**
     * 确认收货或取消订单
     * 40-确认收货 90-取消订单
     *
     * @param status
     */
    public void confirmOrCancel(int status, String orderId) {
        new HttpRequest<DefaultResponse>()
                .with(getActivity()).setApiCode(ApiCst.confirmOrCancelOtherorder)
                .addParam("token", App.getToken(mContext))
                .addParam("order_id", orderId)
                .addParam("status", status)
                .setListener(new HttpRequest.OnNetworkListener<DefaultResponse>() {
                    @Override
                    public void onSuccess(DefaultResponse response) {
                        toast(response.message);
                        recyclerView.refresh();
                    }

                    @Override
                    public void onFail(String message, DefaultResponse response) {
                        toast(message);
                    }
                }).start(new DefaultResponse());

    }


    public void refresh() {
        if (recyclerView != null && adapter != null)
            recyclerView.refresh();
    }

}
